//
//  RightController.m
//  ProjectA
//
//  Created by lanouhn on 15/5/23.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "RightController.h"
#import "ONLY.h"
#import "FirstCenterCell.h"
#import "CellController.h"

@interface RightController ()<UIAlertViewDelegate>
@property (nonatomic ,retain)UILabel *alable;
@property (nonatomic ,retain)UIBarButtonItem *abarRight;
@property (nonatomic ,retain)NSMutableArray *dataSource;
@end

@implementation RightController
- (void)dealloc {
    [_alable release];
    [_abarRight release];
    [super dealloc];
}
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        
//        self.dataSource = [NSKeyedUnarchiver unarchiveObjectWithFile:[[ONLY creatOnly] onlyPath]];
        self.dataSource = [NSMutableArray arrayWithArray:[[ONLY creatOnly] array]];

    
    }
    return _dataSource;
}
- (UILabel *)alable {
    if (!_alable) {
        self.alable = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)]autorelease];
        _alable.text = [NSString stringWithFormat:@"-共%lu条-",(unsigned long)self.dataSource.count];
        _alable.textAlignment = NSTextAlignmentCenter;
        _alable.textColor = [UIColor whiteColor];
    }
    return _alable ;
}
- (UIBarButtonItem *)abarRight {
    if (!_abarRight) {
        self.abarRight = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(handleTrashAction)]autorelease];
        
    }
    return _abarRight ;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.dataSource);
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.titleView = self.alable;
    self.navigationItem.rightBarButtonItem = self.abarRight;
    
    [self.tableView registerClass:[FirstCenterCell class] forCellReuseIdentifier:@"First"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
     _alable.text = [NSString stringWithFormat:@"-共%lu条-",(unsigned long)self.dataSource.count];
    
}
- (void)handleTrashAction {
    UIAlertView *aAlertView = [[UIAlertView alloc]initWithTitle:@"确认" message:@"您确定要清空收藏夹吗？" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    aAlertView.delegate = self;
    [aAlertView show];
    [aAlertView release];
}
#pragma  mark-----------
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        NSLog(@"queding ");
        [(NSMutableArray *)[[ONLY creatOnly] array] removeAllObjects];
        [NSKeyedArchiver archiveRootObject:[[ONLY creatOnly] array] toFile:[[ONLY creatOnly] onlyPath]];
        
        [self.dataSource removeAllObjects];
        [self.tableView reloadData];
        self.alable.text = @"-共0条-";
#warning 清空缓存方法
    }else{
    NSLog(@"quxiao");
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FirstCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:@"First" forIndexPath:indexPath];
    FirstModel *model = self.dataSource[indexPath.row];
    [cell cellWithModel:model temp:YES];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CellController *cellVC = [[CellController alloc]init];
    cellVC.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:cellVC animated:YES];
    [cellVC release];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
     FirstModel *model = self.dataSource[indexPath.row];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
       
        [(NSMutableArray *)[[ONLY creatOnly] array] removeObject:model];
        [NSKeyedArchiver archiveRootObject:[[ONLY creatOnly] array] toFile:[[ONLY creatOnly] onlyPath]];
        [self.dataSource removeObject:model];
        [tableView reloadData];
         _alable.text = [NSString stringWithFormat:@"-共%lu条-",(unsigned long)self.dataSource.count];
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [NSString stringWithFormat:@"确认删除吗？"];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
